﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.ComponentModel;
using System.Drawing.Design;
using TA.JSON;

namespace TA.Turbo.Controls
{
	/// <summary>
	/// Collection of menu items.
	/// </summary>
	public class MenuItemCollection : List<MenuItem>, IJsonCompatible
	{
		/// <summary>
		/// Adds a menu item to the collection.
		/// </summary>
		/// <param name="item"></param>
		public new void Add(MenuItem item)
		{
			base.Add(item);
			item._owner = this;
			//item._click = new ServerEvent<EventArgs>(this.Owner, "Command");

		}

		#region INTERNAL
		#region VARS
		internal object _owner;
		#endregion
		#region PROPS
		internal Menu Owner
		{
			get
			{
				if (_owner is Menu) return (Menu)this._owner;
				if (_owner is MenuItem) return ((MenuItem)this._owner)._owner.Owner;
				return null;
			}
		}
		#endregion
		#region METHODS
		internal void PrepareArguments(string p)
		{
			int index = 0;
			foreach (MenuItem item in this)
			{
				item._click.Set(this.Owner, "Click");
				item._argument = p + index;

				item._items.PrepareArguments(p + index + "-");

				++index;
			}
		}
		#region JSON
		/// <summary>
		/// Gets true if there are any items.
		/// </summary>
		public bool IsDirty
		{
			get { return this.Count > 0; }
		}
		/// <summary>
		/// Serializes this collection by outputting an array.
		/// </summary>
		/// <param name="jw"></param>
		public void Serialize(JsonWriter jw)
		{
			this.SerializeArray(jw);
		}
		/// <summary>
		/// SerializeContents.
		/// </summary>
		/// <param name="jw"></param>
		public void SerializeContents(JsonWriter jw)
		{
			foreach (MenuItem i in this)
				i.Serialize(jw);
		}
		/// <summary>
		/// Deserializes this collection by reading an array.
		/// </summary>
		/// <param name="jr"></param>
		public void Deserialize(IJsonReader jr)
		{
			this.DeserializeArray(jr);
		}
		/// <summary>
		/// DeserializeContent.
		/// </summary>
		/// <param name="jr"></param>
		/// <param name="propShortName"></param>
		public void DeserializeContent(IJsonReader jr, string propShortName)
		{
			MenuItem i = new MenuItem();
			i.Deserialize(jr);
			this.Add(i);
		}
		#endregion
		#endregion
		#endregion
	}
}